package com.yueqian.teachercode;

import java.io.*;

public class TestObjIO {

    public static void main(String[] args) {
        //写账户
        writeAccs("fileDir/accs.txt", new Account("小王", 5000, 2001, 6, 10));

        //读账户
        readAccs("fileDir/accs.txt");

    }

    /**
     * 读账户
     */
    private static void readAccs(String filePath) {
        if (filePath == null || filePath.trim().isEmpty()) {
            System.out.println("ERROR: 输入无效");
            return;
        }
        //封装文件
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("ERROR: 不存在");
            return;
        }

        //文件通道
        FileInputStream fin = null;
        //对象的输入通道
        ObjectInputStream objIn = null;
        try {
            //文件通道关联物理文件
            fin = new FileInputStream(file);
            //对象通道关联文件通道
            objIn = new ObjectInputStream(fin);
            //读取对象
            while (true) {
                Account acc = (Account) objIn.readObject();
                System.out.println(acc);
            }


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (EOFException e) {
            System.out.println("读到文件尾，正常结束...");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (objIn != null) {
                try {
                    objIn.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 写对象
     */
    private static void writeAccs(String filePath, Account acc) {
        if (filePath == null || filePath.trim().isEmpty() || acc == null) {
            System.out.println("ERROR: 输入无效");
            return;
        }
        //封装文件
        File file = new File(filePath);
        //获取路径
        File dir = file.getParentFile();
        //检测路径是否存在，否则建立
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //建立文件通道
        FileOutputStream fout = null;
        //建立对象输出通道
        ObjectOutputStream objOut = null;
        try {
            //文件通道关联物理文件
            fout = new FileOutputStream(file, true);
            //对象通道关联文件通道
            //判断是否有内容
            if (file.length() > 0) {
                //当有内容后，需要重置标记
                objOut = new MyObjectOutputStream(fout);
            } else {
                //当没有内容，写标记
                objOut = new ObjectOutputStream(fout);
            }
            //写对象
            objOut.writeObject(acc);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (objOut != null) {
                    objOut.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if (fout != null) {
                    fout.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    }

}
